package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.domain.CheckGroup;
import com.itheima.domain.CheckItem;
import com.itheima.domain.Setmeal;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //忽略字符串里所有的空格
        if (queryString != null && queryString != "") {
            queryString = queryString.replaceAll(" ","");
        }
        PageHelper.startPage(currentPage,pageSize);

        Page<Setmeal> setmeals = setmealDao.selectCondition(queryString);

        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    /**
     * 添加套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.insertSetmeal(setmeal);
        Integer setmealId = setmeal.getId();
        insertSetmeal(checkgroupIds,setmealId);
        //将保存到数据库的图片名称存入到redis
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);

    }

    private void insertSetmeal(Integer[] checkgroupIds, Integer setmealId) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                setmealDao.addCheckGroupAndSetmeal(map);
            }
        }
    }

    /**
     * 查询所有套餐
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    /**
     * 方式一
     */
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        //新建检查组对象的集合
        List<CheckGroup> checkGroups = new ArrayList<>();
        //查询中间表获取套餐对应的检查组id数组
        Integer[] checkGroupIds = setmealDao.findSetmealAndCheckGroup(id);
        //遍历数组,得到每个检查组的对象
        if (checkGroupIds != null) {
            for (Integer checkGroupId : checkGroupIds) {
                CheckGroup checkGroup = checkGroupDao.findId(checkGroupId);
                //新建检查项对象的集合
                List<CheckItem> checkItems = new ArrayList<>();
                //获取检查组id
                Integer groupId = checkGroup.getId();
                //根据id获取检查组关联的检查项id数组
                Integer[] checkitemIds = checkGroupDao.findCheckGroupAndCheckitem(checkGroupId);
                if (checkitemIds != null) {
                    //遍历数组,得到每个检查项对象
                    for (Integer checkitemId : checkitemIds) {
                        CheckItem checkItem = checkItemDao.selectById(checkitemId);
                        //将检查项添加到检查项集合
                        checkItems.add(checkItem);
                    }
                }
                //将检查项集合放入检查组对象
                checkGroup.setCheckItems(checkItems);
                //将检查组对象添加到检查组集合
                checkGroups.add(checkGroup);
                //将检查组集合放入套餐对象
                setmeal.setCheckGroups(checkGroups);
            }
        }
        return setmeal;
    }

  /*  @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }*/

    @Override
    public List<Map<String, Object>> findSetmealOrderCount() {
        return setmealDao.findSetmealOrderCount();
    }

    @Override
    public List<Map<String, Object>> findAllAddress() {

        return setmealDao.findAllAddress();
    }
}
